<?php

class Application_Model_Genset extends Zend_Db_Table_Abstract
{
    protected $_name = 'tabel_genset';
    
    public function gensetBaru($data)
    {
        $this->insert($data);
    }
    
    public function gensetUpdate($data, $id)
    {
        $this->update($data, 'id_genset = '.$id);
    }
    
    public function gensetDelete($id)
    {
        $this->delete('id_genset = '.(int)$id);
    }


    public function getDataJoin()
    {
        $select = $this->select();
        $select->setIntegrityCheck(false)
               ->from($this->_name)
               ->joinLeft('tabel_spek_engine', 'tabel_genset.id_engine_genset = tabel_spek_engine.id_engine_genset')
               ->joinLeft('tabel_supplier', 'tabel_genset.id_supplier = tabel_supplier.id_supplier')
               ->joinLeft('tabel_harga_sewa', 'tabel_genset.id_harga_sewa = tabel_harga_sewa.id_harga_sewa');
        
        $result = $this->fetchAll($select)->toArray();
        return $result;
    }
    
    public function getDataJoinId($id)
    {
        $select = $this->select();
        $select->setIntegrityCheck(false)
               ->from($this->_name)
               ->where('id_genset = ?', $id)
               ->joinLeft('tabel_spek_engine', 'tabel_genset.id_engine_genset = tabel_spek_engine.id_engine_genset')
               ->joinLeft('tabel_supplier', 'tabel_genset.id_supplier = tabel_supplier.id_supplier')
               ->joinLeft('tabel_harga_sewa', 'tabel_genset.id_harga_sewa = tabel_harga_sewa.id_harga_sewa')
               ->joinLeft('tabel_proyek', 'tabel_genset.id_proyek = tabel_proyek.id_proyek');
        
        $result = $this->fetchRow($select)->toArray();
        if(!$result)
            return false;
        else
            return $result;
    }
        
    public function getData($id)
    {
        $select = $this->select();
        $select->from($this->_name)
               ->setIntegrityCheck(false)
               ->joinLeft('tabel_proyek', 'tabel_proyek.id_proyek = tabel_genset.id_proyek')
               ->joinLeft('tabel_spek_engine','tabel_genset.id_engine_genset = tabel_spek_engine.id_engine_genset')
               ->where('tabel_genset.id_proyek = ?', $id);
        
        $result = $this->fetchAll($select);
        
        if($result)
            return $result->toArray ();
        else
            return false;
    }
    
    public function getDataTerpakai()
    {
        $select = $this->select();
        $select->setIntegrityCheck(false)
               ->from($this->_name)
               ->where('status_genset = ?','Terpakai')
               ->joinLeft('tabel_spek_engine', 'tabel_genset.id_engine_genset = tabel_spek_engine.id_engine_genset');
        
        $result = $this->fetchAll($select)->toArray();
        return $result;
    }
    
    public function getGensetBelumTerpakai($id)
    {
        $select = $this->select();
        $select->setIntegrityCheck(false)
               ->from($this->_name)
               ->where('status_genset = ?','Belum Terpakai')
               ->where('id_genset = ?',$id)
               ->joinLeft('tabel_spek_engine', 'tabel_genset.id_engine_genset = tabel_spek_engine.id_engine_genset');
        
        $result = $this->fetchAll($select)->toArray();
        if(!$result)
            return false;
        else
            return true;
    }
}